package structure;

import Tree.MyBinaryTree;

import java.util.*;

public class Test10 extends MyBinaryTree{
    public static void main(String[] args) {

    }

    //leetcode 下一个更大元素2
    public int[] nextGreaterElements(int[] nums) {
        int len = nums.length;
        int[] arr = new int[len];
        Arrays.fill(arr,-1);
        int num = 0;
        Deque<Integer> stack = new ArrayDeque<>();
        for (int i = 0; i < len; i++) {
            while (!stack.isEmpty() && nums[stack.peek()] < nums[i]) {
                int index = stack.pop();
                arr[index] = nums[i];
            }
            if (num == 0) {
                stack.push(i);
            }
            if (i == len - 1 && num == 0) {
                i = -1;
                num++;
                continue;
            }
        }
        return arr;
    }    //蓝桥杯 神秘符文的重复序列
    public static void main0(String[] args) {
        Scanner in = new Scanner(System.in);
        long num = 0;
        int N = in.nextInt();
        int K = in.nextInt();
        long b = 0;
        int a = 0;
        String s = in.next();
        int len = s.length();
        for(int i = 0;i < len;i++) {
            if (s.charAt(i) == 'b') {
                b++;
            }
        }
        long b0 = 0;
        for(int i = 0;i < len;i++){
            if(s.charAt(i) == 'b'){
                b0++;
            }
            if(s.charAt(i) == 'a'){
                num += b * K * (K + 1)/2 - b0 * K;
            }
        }
        System.out.println(num);
    }
}
